# 字典是可变的数据结构

person = {'name':'龙仔','age':18,'meath':98,'Chinese':95}
# print(person)
# 创建字典对象
"""
a = {"name":"龙仔",'age':18,'job':"测试"}
print(a)

b = dict(name='龙仔',age = 18,job='测试')
print(b)
"""
# 通过zip() 创建字典对象
"""
k = ['name','age','job']
v = ['longzai',18,'techer']
d = dict(zip(k,v))
print(d)
"""
# 通过fromkeys创建值为空的字典
"""
a = dict.fromkeys(['name','age','job'])
print(a)
"""
# 字典增删改查
# 字典查
dict1 = {"姓名":"关博文","年龄":19,"班级":"1405A"}
"""
print(dict1['姓名']) # 关博文
print(dict1['年龄']) # 19

print(dict1.get('姓名')) # 关博文 , 通过字典的内置方法，来获取姓名
print(dict1.values()) # 获取全部的值 :dict_values(['关博文', 19, '1405A'])
print(dict1.items()) # 返回全部的键值对
print(dict1.keys()) # 返回全部的键：dict_keys(['姓名', '年龄', '班级'])
print(len(dict1)) # 3
"""
# 字典添加
"""
dict1.update({"地址":"辽宁沈阳"})
print(dict1)
dict1['性别'] = "男"
print(dict1)
dict1.update({"姓名":"李禹浩"}) # 增加的是已经存在的key、value就会是修改
print(dict1)
"""
# 字典删除
"""
result = dict1.pop("年龄") # 删除年龄的键值对
print(dict1)
print(f"被删除的对象是{result}")

result1 = dict1.popitem() # 随机删除一组键值对
print(dict1)
print(f"被删除的对象是{result1}")

del dict1['姓名'] # 通过del方法删除字典的键值对
print(dict1)

dict2 = {"工作":"测试"}
dict2.clear() # 清空字典全部内容
print(dict2)
"""
# 字典的遍历
"""
for i in dict1: # 直接遍历字典，是访问字典的key
    print(i)
"""
"""
for i in dict1.values(): # 使用字典的values方法，遍历访问value值
    print(i)
"""
"""
for i in dict1.keys(): # 遍历访问字典的key值
    print(i)
"""
"""
for key, value in dict1.items(): # 遍历字典的键值对
    print(key,value)
"""
# 字典推导式
"""
dict2 = {k:v for k,v in {'name':"小刘",'age':[18]}.items()}
print(dict2) # {'name': '小刘', 'age': [18]}
"""

# 案例
# 统计字符出现的次数_并打印出最多出现的字符
"""
chars = ['a','c','x','d','p','a','p','a','c']
dict_result = {}
for i in chars:
    dict_result[i] = chars.count(i)
for key,value in dict_result.items():
    if max(dict_result.values()) == value:
        print(f"最大的字符是{key},出现次数为：{value}")
"""

# 字典解包 （打包和解包并不能脱离函数而存在）
"""
c = {"name": 'zhang', "age": 2}
# print(**c) # TypeError: 'name' is an invalid keyword argument for print()
print("Name:{name},Age:{age}".format(**c))
"""









